#!/bin/bash

# Version: 2011-03-24
# Author: Daniele Paolella <danielepaolella@email.it>

settings=$(disper -pv 2>&1) || exit 1

# temporarily change the Internal Field Separator to make an array out of a
# comma-separated list
oifs=$IFS
IFS=', '
displays=($(echo "$settings" | grep 'auto-detected displays' | cut -d : -f 2))
IFS=$oifs

# double echo do whitespace trimming
current=$(echo $(echo "$settings" | grep metamode | cut -d : -f 2 ))

i=1
for d in "${displays[@]}" ; do
    if [ "$d" = "$current" ]; then
        if [ $i -eq ${#displays[@]} ]; then
            i=0
        fi
        next=${displays[$i]}
        break
    fi
    let i="$i + 1"
done

disper -d "$next" -c
